import java.awt.Color;
import java.awt.Graphics;
import java.util.Map;

import com.speljohan.rsbot.bot.Bot;
import com.speljohan.rsbot.event.listeners.PaintListener;
import com.speljohan.rsbot.script.Constants;
import com.speljohan.rsbot.script.Script;
import com.speljohan.rsbot.script.wrappers.RSInterface;
import com.speljohan.rsbot.script.wrappers.RSItem;
import com.speljohan.rsbot.script.wrappers.RSItemTile;
import com.speljohan.rsbot.script.wrappers.RSNPC;
import com.speljohan.rsbot.script.wrappers.RSObject;
import com.speljohan.rsbot.script.wrappers.RSTile;

public class DagannothKiller extends Script implements PaintListener {
	RSTile LADDER_UP_TILE = new RSTile(2515, 10006);
	int eatAT = 40;
	int amount;
	public int fightMode;
	public long startTime = System.currentTimeMillis();
	public long time = System.currentTimeMillis();
	public int ITPID1 = 0000;
	public int ITPID2 = 0000;
	int drags[] = { 1341, 1340, 1338, 1343, 1342, 1342, 1339 };
	public boolean pickoi = false;
	public boolean doingspec = false;
	RSTile NEED_FOOD_TILE = new RSTile(2513, 10002);
	RSTile HAVE_FOOD_TILE = new RSTile(2516, 10003);
	int height = getPlane();
	RSTile NEED_TO_GET_OUT = new RSTile(2515, 10006);
	int LADDER = 4485;
	int LADDER_DOWN = 4383;
	RSTile STAIRS_UP = new RSTile(2506, 3641);
	int Seller = 1334;
	int fightMode2;
	int EVIL[] = { 2463, 2464, 2465, 2466, 2467, 2468, 3366, 3367, 3368, 3369,
			3370, 3371, 3375 };
	RSTile IS_THIS_TILE = new RSTile(2515, 10004);
	RSTile IS_THIS_TILE2 = new RSTile(2519, 9995);
	RSTile LADDER_UP = new RSTile(2519, 9994);
	RSTile[] Path1 = { new RSTile(2516, 10000), new RSTile(2519, 9996) };
	RSTile[] Path2 = { new RSTile(2526, 10027), new RSTile(2519, 10020),
			new RSTile(2516, 10011) };
	int FoodID = 0000;
	public long lvls = 0;
	int attacking;
	public int[] ITPIDs;

	Color BG = new Color(0, 0, 0, 125);

	void doSpec() {
		if (getSpec() == 1000 && !specEnabled() && doingspec) {
			openTab(Constants.TAB_ATTACK);
			clickMouse(645 + random(0, 4), 425 + random(0, 4), true);
			wait(random(500, 1000));
		} else {
			wait(random(1, 3));
		}
	}

	void eat(final int id) {
		atInventoryItem(id, "Eat");
		wait(random(1500, 2500));
		return;
	}

	private void eat_check() {
		final RSItem food = getInventoryItemByID(FoodID);
		if (getInventoryCount(FoodID) >= 1) {
			eat(food.getID());
		} else {
			log("why is this here? idk");
		}
		return;
	}

	void EATING() {
		final int php = skills.getCurrentSkillLevel(3);
		if (php <= eatAT) {
			eat_check();
		} else {
			wait(random(20, 30));
		}
	}

	@Override
	public String getAuthor() {
		return "Killa";
	}

	@Override
	public String getName() {
		return "Dagonnoth Killer";
	}

	@Override
	public String getScriptCategory() {
		return "Combat";
	}

	@Override
	public String getScriptDescription() {
		String html = "";
		html += "<html>";
		html += "<head>";
		html += "<style type=\"text/css\"> hr {color: white} p {margin-left: 20px}</style>";
		html += "</head>";
		html += "<body>";
		html += "<center>";
		html += "<b><font size=\"6\" color=\"red\">" + getName() + " v"
				+ getVersion() + " c" + "</font></b><br>";
		html += "<font size=\"3\" color=\"red\">Light House @ Dragannoths.<br></font>";
		html += "</center>";
		html += "<center><table border=\"0\"><tr><td colspan=\"5\"><center><font size=6><b>== Options and Settings ==</b></font></center></td></tr><tr><td colspan=4><hr></td></tr><tbody><tr><td width=\"135\">";
		html += "<b>Use Special:</b></td><td width=\"140\"><center><input type=\"checkbox\" name=\"spec\" value=\"true\" checked=\"checked\">&nbsp<b>Yes</b></center></td><td width=\"135\"></b></center></td></tr>";
		html += "<td><center><select name=\"attT\"><option>Strength<option selected>Attack<option>Defence<option ></center></td></tr>";
		html += "<tr><td><b>Pick Items:</b></td><td colspan=\"3\"><center><b>Item ID 1:</b>&nbsp<input type=\"text\" name=\"ItemID1\" size=\"4\">&nbsp&nbsp<b>Item ID 2:</b>&nbsp<input type=\"text\" name=\"ItemID2\" size=\"4\"></center></td></tr>";
		html += "<tr><td><b>Eat Settings:</b></td>";
		html += "<tr><td><b>Food Noted:</b>&nbsp<input type=\"text\" name=\"amount\" size=\"4\"><center><b>Eat At(40 defult):</b>&nbsp<input type=\"text\" name=\"eatt\" size=\"4\"</center><b>Food Unoted:</b>&nbsp<input type=\"text\" name=\"amount2\" size=\"4\"><center></td></tr>";
		html += "<tr><td colspan=2><b><font size=\"3\">&nbsp&nbsp Beta! Enjoy!</font></b></td><td  colspan=\"2\" align=right>"
				+ getVersion() + " &nbsp&nbsp</td></tr></table>";
		html += "</center></body>";
		html += "</html>";
		return html;
	}

	public int getSpec() {
		return getSetting(300);
	}

	@Override
	public double getVersion() {
		return 1.5;
	}

	void INV() {
		if (getInventoryCount() <= 17) {
			atInventoryItem(amount, "10");
			wait(random(2000, 4000));
			final int x = random(77, 81), y = random(98, 112);
			clickMouse(x, y, false);
			atMenu("50");
		} else {
			final int x = random(482, 489), y = random(45, 40);
			clickMouse(x, y, true);
		}
	}

	private boolean isThere2() {
		return getMyPlayer().getLocation().getY() <= 9997;
	}

	void Lad() {
		final RSObject Laddering = findObject(LADDER_DOWN);
		if (findObject(LADDER_DOWN) != null) {
			atObject(Laddering, "Climb");
		} else {
			wait(random(50, 100));
		}
	}

	void LOOL() {
		if (findObject(4545) == null) {
			wait(random(500, 1000));
		} else {
			atObject(findObject(4545), "Open");
		}
	}

	@Override
	public int loop() {
		setFightMode(fightMode);
		if (getEnergy() > random(60, 100)) {
			setRun(true);
		}
		if (getInventoryCount(FoodID) >= 2 && findObject(LADDER_DOWN) != null) {
			setCompass('e');
			Lad();
			wait(random(1000, 2000));
		}
		if (Wrong() && findObject(31130) != null) {
			walkTo(NEED_FOOD_TILE);
		}
		final int[] NEEDED = { ITPID1, ITPID2, FoodID, 4587, 3863, 3853, 3865,
				3867, 3859, 3857, 3855, 3861, 995, amount };
		if (getMyPlayer().getLocation().equals(HAVE_FOOD_TILE)
				&& getInventoryCount(FoodID) >= 2 && findObject(LADDER) != null) {
			setCameraRotation(135);
			setCameraAltitude(true);
			atObject(findObject(LADDER), "Climb");
		}
		if (getMyPlayer().getLocation().equals(IS_THIS_TILE)
				|| getMyPlayer().getLocation().equals(new RSTile(2515, 10005))
				&& getInventoryCount(FoodID) <= 2 && findObject(4546) != null) {
			setCompass('s');
			setCameraAltitude(false);
			atObject(findObject(4546), "Open");
		}
		if (getMyPlayer().getLocation().equals(NEED_FOOD_TILE)) {
			walkPathMM(Path1, 20);
			wait(random(1000, 1200));
		}
		if (getMyPlayer().getLocation().equals(new RSTile(2519, 9996))
				&& !getMyPlayer().isMoving()) {
			MakingSure();
		}
		if (getMyPlayer().getLocation().equals(IS_THIS_TILE2)
				&& getInventoryCount(FoodID) >= 2) {
			walkTo(HAVE_FOOD_TILE);
			wait(random(1500, 2000));
			setCompass('N');
			setCameraAltitude(false);
			LOOL();
		}
		if (getMyPlayer().getInteracting() == null
				&& getInventoryCount(FoodID) >= 2
				&& getNearestNPCByID(drags) != null
				&& getNearestNPCByID(EVIL) == null
				&& getNearestNPCByName("Swarm") == null) {
			doSpec();
			setCameraAltitude(true);
			final RSNPC DAG = getNearestFreeNPCByID(drags);
			atNPC(DAG, "Attack");
			wait(random(1000, 2000));
		}
		if ((getMyPlayer().getInteracting() == null || getMyPlayer()
				.getInteracting() != null)
				&& getInventoryCount(FoodID) >= 2
				&& getNearestNPCByID(drags) != null
				&& (getNearestNPCByID(EVIL) != null || getNearestNPCByName("Swarm") != null)) {
			walkPathMM(randomizePath(Path2, 2, 2), 20);
			EATING();
			wait(random(1500, 3000));
		}
		if (getInventoryCount(FoodID) <= 1 && getNearestNPCByID(Seller) != null) {
			setCameraAltitude(true);
			final RSNPC NOOB = getNearestNPCByID(Seller);
			atNPC(NOOB, "Trade");
			wait(random(500, 1000));
			Trading();
		}
		if (getInventoryCount(FoodID) >= 2 && getNearestNPCByID(Seller) != null) {
			atObject(getObjectAt(STAIRS_UP), "Climb-down");
		}
		if (getInventoryCount(FoodID) <= 1
				&& getMyPlayer().getLocation().equals(new RSTile(2510, 3644))
				&& getObjectAt(STAIRS_UP) != null) {
			atObject(getObjectAt(STAIRS_UP), "Climb-up");
		}
		if (getInventoryCount(FoodID) <= 1 && getNearestNPCByID(drags) != null) {
			setCompass('s');
			setCameraAltitude(false);
			walkPathMM(randomizePath(Path2, 2, 2), 20);
			atObject(getObjectAt(NEED_TO_GET_OUT), "Climb");
			wait(random(1500, 3000));
		}
		if (getMyPlayer().getInteracting() != null
				&& getInventoryCount(FoodID) >= 2
				&& getNearestNPCByID(drags) != null
				&& getNearestNPCByID(EVIL) == null
				&& getNearestNPCByName("Swarm") == null) {
			doSpec();
			dropAllExcept(NEEDED);
			pickOI();
			EATING();
			return random(100, 200);
		}
		if (tileOnScreen(new RSTile(2970, 3341))) {
			logout();
			stopAllScripts();
		}
		if (getInventoryCount(3062) >= 2) {
			setCompass('s');
			setCameraAltitude(false);
			walkPathMM(randomizePath(Path2, 2, 2), 20);
			atObject(getObjectAt(NEED_TO_GET_OUT), "Climb");
			wait(random(1500, 3000));
			logout();
			stopAllScripts();
		}
		return random(10, 20);
	}

	void MakingSure() {
		if (isThere2() && !getMyPlayer().isMoving()) {
			atObject(getObjectAt(LADDER_UP), "Climb");
		} else {
			wait(random(50, 100));
		}
	}

	@Override
	public void onFinish() {
		Bot.getEventManager().removeListener(PaintListener.class, this);
	}

	public void onRepaint(final Graphics g) {
		if (isLoggedIn()) {
			long millis = System.currentTimeMillis() - startTime;
			final long hours = millis / (1000 * 60 * 60);
			millis -= hours * 1000 * 60 * 60;
			final long minutes = millis / (1000 * 60);
			millis -= minutes * 1000 * 60;
			final long seconds = millis / 1000;
			g.setColor(BG);
			g.fill3DRect(9, 210, 170, 120, true);
			g.setColor(Color.white);
			g.drawString("Dagonnoth Killer by killa", 18, 230);
			g.drawString("EXP Gained: "
					+ (skills.getCurrentSkillExp(fightMode2) - attacking), 18,
					250);
			g.drawString("EXP To next lvl Now: "
					+ skills.getXPToNextLevel(fightMode2), 18, 270);
			g.drawString("Running for: " + hours + "h " + minutes + "m "
					+ seconds + "s", 18, 290);
			g.drawString("Enjoy The Script!!!!", 18, 310);
		}
	}

	@Override
	public boolean onStart(final Map<String, String> args) {
		if (!args.get("eatt").equals("")) {
			eatAT = Integer.parseInt(args.get("eatt"));
		}
		try {
			log(args.get("amount"));
			amount = Integer.parseInt(args.get("amount"));
		} catch (final Exception e) {
			log("Invalid Food Noted");
			return false;
		}
		try {
			log(args.get("amount2"));
			FoodID = Integer.parseInt(args.get("amount2"));
		} catch (final Exception e) {
			log("Invalid Food");
			return false;
		}
		if (args.get("attT").equals("Strength")) {
			attacking = skills.getCurrentSkillExp(2);
			fightMode = 1;
		}
		if (args.get("attT").equals("Attack")) {
			attacking = skills.getCurrentSkillExp(0);
			fightMode = 0;
		}
		if (args.get("spec") != null) {
			if (args.get("spec").equals("true")) {
				doingspec = true;
			}
		}
		if (args.get("attT").equals("Defence")) {
			attacking = skills.getCurrentSkillExp(1);
			fightMode = 3;
		}
		if (fightMode == 3) {
			fightMode2 = 1;
		}
		if (fightMode == 0) {
			fightMode2 = 0;
		}
		if (fightMode == 1) {
			fightMode2 = 2;
		}
		if (!args.get("ItemID1").equals("")) {
			ITPID1 = Integer.parseInt(args.get("ItemID1"));
			pickoi = true;
		}
		if (!args.get("ItemID2").equals("")) {
			ITPID2 = Integer.parseInt(args.get("ItemID2"));
			pickoi = true;
		}
		ITPIDs = new int[] { ITPID1, ITPID2, };
		return true;
	}

	public void pickOI() {
		getInventoryItemByID(ITPIDs);
		final int OldOitemCount = getInventoryCount(ITPIDs);
		final RSItemTile TItem = getGroundItemByID(5, ITPIDs);
		if (TItem != null) {
			atTile(TItem, "Take");
			while (getMyPlayer().isMoving()) {
				wait(15);
			}
			if (getInventoryCount(ITPIDs) > OldOitemCount) {
				log("Picked up item.");
				return;
			} else {
				return;
			}
		}
		return;
	}

	void skillCape() {
		if (skills.getCurrentSkillLevel(Constants.STAT_STRENGTH) < 100) {
			openTab(Constants.TAB_EQUIPMENT);
			wait(random(200, 300));
			clickMouse(601 + random(-4, 4), 267 + random(-4, 4), false);
			atMenu("Operate");
		} else {
			wait(random(1, 3));
		}
	}

	boolean specEnabled() {
		return getSetting(301) == 1;
	}

	void Trading() {
		if (RSInterface.getInterface(620).isValid()) {
			INV();
			wait(random(500, 1000));
			INV();
			wait(random(3000, 4000));
			final int x2 = random(482, 489), y2 = random(45, 40);
			clickMouse(x2, y2, true);
		} else {
			final RSNPC Noob = getNearestNPCByID(Seller);
			atNPC(Noob, "Trade");
		}
	}

	private boolean Wrong() {
		return getMyPlayer().getLocation().getY() <= 9994;
	}
}